package com.example.tools;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class LoadJSON{
    public static class gismu implements Parcelable {
        public String name,ciXing,meaning;
        public Map<String,String> sentences;

        protected gismu(Parcel in) {
            sentences=new HashMap<>();
            in.readMap(sentences,HashMap.class.getClassLoader());
            String[] cache=new String[]{"\0","\0","\0"};
            in.readStringArray(cache);
            name=cache[0];
            ciXing=cache[1];
            meaning=cache[2];
        }
        public gismu(){}

        public static final Creator<gismu> CREATOR = new Creator<gismu>() {
            @Override
            public gismu createFromParcel(Parcel in) {
                return new gismu(in);
            }

            @Override
            public gismu[] newArray(int size) {
                return new gismu[size];
            }
        };

        /**
         * Describe the kinds of special objects contained in this Parcelable
         * instance's marshaled representation. For example, if the object will
         * include a file descriptor in the output of {@link #writeToParcel(Parcel, int)},
         * the return value of this method must include the
         * {@link #CONTENTS_FILE_DESCRIPTOR} bit.
         *
         * @return a bitmask indicating the set of special object types marshaled
         * by this Parcelable object instance.
         */
        @Override
        public int describeContents() {
            return 0;
        }

        /**
         * Flatten this object in to a Parcel.
         *
         * @param dest  The Parcel in which the object should be written.
         * @param flags Additional flags about how the object should be written.
         *              May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
         */
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeMap(sentences);
            dest.writeStringArray(new String[]{name, ciXing, meaning});
        }
        @Override
        public String toString(){
            return this.name;
        }//don't add @
    }
    public List<gismu> dictionary;
    public void load(InputStream fis) throws JSONException, IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(fis));
        StringBuilder sb=new StringBuilder();
        String count;
        while((count=br.readLine())!=null){
            sb.append(count);
        }
        JSONObject jsonObject=new JSONObject(sb.toString());
        dictionary=new ArrayList<>();
        for (Iterator<String> it = jsonObject.keys(); it.hasNext(); ) {
            String key = it.next();
            gismu cache = new gismu();
            cache.name=key;
            JSONArray jsonArray=jsonObject.optJSONArray(key);
            assert jsonArray != null;
            cache.ciXing=jsonArray.optString(0,"null");
            cache.meaning=jsonArray.optString(1,"null");
            JSONObject jsonObject1=jsonArray.optJSONObject(2);
            if(jsonObject1==null){
                jsonObject1=new JSONObject();
                jsonObject1.put("null%","null");
            }
            cache.sentences=new HashMap<>();
            for (Iterator<String> i=jsonObject1.keys();i.hasNext();){
                String sentence=i.next();
                cache.sentences.put(sentence,jsonObject1.optString(sentence));
            }
            if(cache.sentences.isEmpty()){
                cache.sentences.put("null%","null");
            }
            dictionary.add(cache);
        }
    }
    public gismu findGismu(String word){
        for(gismu i:dictionary){
            if(i.name.equals(word)){
                return i;
            }
        }
        return null;
    }
}
